<?php 
class User_model extends CI_Model
{
	var $member=array();
	public function __construct()
	{
	}
	
	public function registration($activatekey){
		$fname=htmlspecialchars(strip_tags($this->input->post('fname')),ENT_QUOTES);
		$lastname=htmlspecialchars(strip_tags($this->input->post('lastname')),ENT_QUOTES);
		$email=htmlspecialchars(strip_tags($this->input->post('email')),ENT_QUOTES);
		$country=htmlspecialchars(strip_tags($this->input->post('country')),ENT_QUOTES);
		$password=htmlspecialchars(strip_tags($this->input->post('password')),ENT_QUOTES);
		
		$data=array(
				'user_first_name'=>ucwords($fname),
				'user_last_name'=>ucwords($lastname),
				'user_email'=>$email,
				'user_country'=>$country,
				'user_password'=>md5($password),
				'user_activation_code'=>$activatekey,
				'user_created_date'=>date('Y-m-d H:i:s'),
				'user_status'=>'Inactive'
			);
		$query=$this->db->insert('scopus_user',$data);
		if($query){
			return true;
		}
		else {
			return false;
		}
	}
	
	public function activate_user($key){
		$resetkey= md5(uniqid());
		$data= array (
					'user_activation_code'=>'NULL',
					'user_reset_code'=>$resetkey,
					'user_status'=>'active'
				);
				
		$this->db->where('user_activation_code',$key);
		$query=$this->db->get('scopus_user');
		if($query->num_rows()==1){
			$result=$query->result();
			$this->db->where('user_id',$result[0]->user_id);
			$update=$this->db->update('scopus_user',$data);
			if($update){
				$this->db->where('user_id',$result[0]->user_id);
				$result=$this->db->get('scopus_user');
				return $result->result();
			}else {
				return false;
			}
		}
		else {
			return false;
		}
	}
	
	public function getinfo(){
		$this->db->where('user_id',$this->session->userdata('user_id'));
		$query=$this->db->get('scopus_user');
		if($query->num_rows()==1){
			return $query->result();
		}
	}
	
	public function getcountry(){
		$this->db->where('country_status','Active');
		$query=$this->db->get('scopus_master_country');
		if($query){
			return $query->result();
		}
		else {
			return false;
		}
	}
	
	public function getcountryById($id){
		$this->db->where('country_id',$id);
		$this->db->where('country_status','Active');
		$query=$this->db->get('scopus_master_country');
		if($query){
			return $query->result();
		}
		else {
			return false;
		}
	}
	
	public function getawards(){
	//	if(($this->session->userdata('application_event_id') && $this->session->userdata('application_award_id'))){ 
			$this->db->where('award_event_id',$this->session->userdata('application_event_id'));
			$this->db->where('award_id',$this->session->userdata('application_award_id'));
	//	}
		$this->db->where('award_status','Active');
		$this->db->where('award_current_status !=','Done');
		
		$query=$this->db->get('scopus_award');
		if($query){
			return $query->result();
		}
		else {
			return false;
		}
	}
	
	public function getCurrentAwards(){
		
			$this->db->where('award_event_id',$this->session->userdata('application_event_id'));
			$this->db->where('award_id',$this->session->userdata('application_award_id'));
		
		$this->db->where('award_status','Active');
		$this->db->where('award_current_status !=','Done');
		//$where = "(award_current_status!='Done') AND (award_status='Active')";
		//$this->db->where($where);
		$query=$this->db->get('scopus_award');
		if($query){
			return $query->result();
		}
		else {
			return false;
		}
	}
	
	public function edit_details(){
		//htmlspecialchars(strip_tags($this->input->post('')),ENT_QUOTES);
		$fname = htmlspecialchars(strip_tags($this->input->post('fname')),ENT_QUOTES);
		$lname = htmlspecialchars(strip_tags($this->input->post('lname')),ENT_QUOTES);
		$Country = htmlspecialchars(strip_tags($this->input->post('Country')),ENT_QUOTES);
		$phone = htmlspecialchars(strip_tags($this->input->post('phone')),ENT_QUOTES);
		$data = array(
			'user_first_name'=>$fname,
			'user_last_name'=>$lname,
			'user_country'=>$Country,
			);
		$npassword = htmlspecialchars(strip_tags($this->input->post('npassword')),ENT_QUOTES);
		if($npassword!=''){
			$data['user_password']=md5($this->input->post('npassword'));
		}
		if(isset($_FILES['userfile']) && $_FILES['userfile']['name']!='')	{
			$data['user_profile_photo']= BASE_URL().'files/applicant_photo/'.$this->upload->file_name;
		}
		$this->db->where("user_id", $this->session->userdata('user_id'));
		$query = $this->db->update('scopus_user',$data);
		if($query){
			return true;
		}
		else {
			return false;
		}
	}
	
	public function refree_details(){
		$name = htmlspecialchars(strip_tags($this->input->post('name')),ENT_QUOTES);
		$email = htmlspecialchars(strip_tags($this->input->post('email')),ENT_QUOTES);
		$phone = htmlspecialchars(strip_tags($this->input->post('phone')),ENT_QUOTES);
		$city = htmlspecialchars(strip_tags($this->input->post('city')),ENT_QUOTES);
		$state = htmlspecialchars(strip_tags($this->input->post('state')),ENT_QUOTES);
		$Country = htmlspecialchars(strip_tags($this->input->post('Country')),ENT_QUOTES);
		$zip_code = htmlspecialchars(strip_tags($this->input->post('zip_code')),ENT_QUOTES);
		$position = htmlspecialchars(strip_tags($this->input->post('position')),ENT_QUOTES);
		$affiliation = htmlspecialchars(strip_tags($this->input->post('affiliation')),ENT_QUOTES);
		$Address1 = htmlspecialchars(strip_tags($this->input->post('Address1')),ENT_QUOTES);
		$Address2 = htmlspecialchars(strip_tags($this->input->post('Address2')),ENT_QUOTES);
		$Address3 = htmlspecialchars(strip_tags($this->input->post('Address3')),ENT_QUOTES);
		$Comments1 = htmlspecialchars(strip_tags($this->input->post('Comments1')),ENT_QUOTES);
		$Comments2 = htmlspecialchars(strip_tags($this->input->post('Comments2')),ENT_QUOTES);
		$Comments3 = htmlspecialchars(strip_tags($this->input->post('Comments3')),ENT_QUOTES);
		$Comments4 = htmlspecialchars(strip_tags($this->input->post('Comments4')),ENT_QUOTES);
		$Comments5 = htmlspecialchars(strip_tags($this->input->post('Comments5')),ENT_QUOTES);
		$Comments6 = htmlspecialchars(strip_tags($this->input->post('Comments6')),ENT_QUOTES);
		$Comments7 = htmlspecialchars(strip_tags($this->input->post('Comments7')),ENT_QUOTES);
		$Address = $Address1;	
		if($Address2!=''){
			$Address = $Address.' , '.$Address2;
		}
		if($Address3!=''){
			$Address = $Address.', '.$Address3;
		}
		
		$data = array (
					'refree_name'=>$name,
					'refree_position'=>$position,
					'refree_institutional_affiliation'=>$affiliation,
					'refree_postal_code'=>$zip_code,
					'refree_state'=>$state,
					'refree_country'=>$Country,
					'refree_address'=>$Address,
					'refree_phone'=>$phone,
					'refree_email'=>$email,
					'refree_created_date'=>date('Y-m-d H:i:s'),
					'refree_status' => 'Active'
				);
			$this->db->insert('scopus_refree',$data);	
			$insert_id = $this->db->insert_id();
			if($this->input->post('accept')){
				$refree_review_verify_status='Verified';
			}else {
				$refree_review_verify_status='Not Verified';
			}
			$data_review = array (
					'refree_review_refree_id'=>$insert_id,
					'refree_review_ques1'=>'How long have you known the applicant and in what capacity?',
					'refree_review_ques2'=>'Are you familiar with the applicant’s research? Please explain',
					'refree_review_ques3'=>'Please comment on the applicant’ most significant contributions to research in his/her field.',
					'refree_review_ques4'=>'Please comment on the applicant’s potential to make future long term contributions in his/her field.',
					'refree_review_ques5'=>'Please comment on the extent to which the applicant’s research has had an impact outside its field of study and academia generally, including any positive change (social/economic/technological etc) brought about by, or any interventions induced by, the applicant’s research.',
					'refree_review_ques6'=>'Please comment on the applicant’s potential to make significant long-term contributions to social, economic and/or technological benefits for the regional, national, and/or global community.',
					'refree_review_ques7'=>'Additional comments concerning the applicant’s merit for this award',
					'refree_review_ans1'=>$Comments1,
					'refree_review_ans2'=>$Comments2,
					'refree_review_ans3'=>$Comments3,
					'refree_review_ans4'=>$Comments4,
					'refree_review_ans5'=>$Comments5,
					'refree_review_ans6'=>$Comments6,
					'refree_review_ans7'=>$Comments7,
					'refree_review_verify_status'=>$refree_review_verify_status,
					'refree_review_created_date'=>date('Y-m-d H:i:s'),
					'refree_review_status'=>'Active'
					);
			
			$this->db->insert('scopus_refree_review',$data_review);
			return true;
	}

	public function getFormList($value='', $type='')
	{
		if (empty($value))
		{
			return FALSE;
		}
		$q= $this->db->where('award_id',$value);
		$q = $this->db->get('scopus_award');
		$q_r = $q->result_array();
		$event_id = $q_r[0]['award_event_id'];
		$sql_select = array('*');
		$sql_whrere = "(app.event_application_form_field_id = master.master_application_id) AND
				(app.event_application_form_event_id = '".$event_id."') AND 
				(app.event_application_form_category = '".$type."') AND 
				(app.event_application_form_status = 'Active')";
		$from = 'event_application_form app, master_application_form master';
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix($from));
	    $resultData=array();
		if($query->num_rows()>0){
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	public function getNominationFormList($event='', $type='')
	{
		if (empty($event))
		{
			return FALSE;
		}
		$event_id = $event;
		$sql_select = array('*');
		$sql_whrere = "(app.event_nomination_form_field_id = master.master_nomination_id) AND
				(app.event_nomination_form_event_id = '".$event_id."') AND 
				(app.event_nomination_form_category = '".$type."') AND 
				(app.event_nomination_form_status = 'Active')";
		$from = 'event_nomination_form app, master_nomination_form master';
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix($from));
	    $resultData=array();
		if($query->num_rows()>0){
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	public function getFormData($value='')
	{
		if (empty($value))
		{
			return FALSE;
		}
		$sql_select = array('*');
		$sql_whrere = "(e.event_id=a.award_event_id) AND (a.award_id='".$value."') AND (award_status='Active')";
		$from = 'event e, award a';
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix($from));
	    $resultData=array();
		if($query->num_rows()>0){
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	public function checkFormSubmit($value='', $id='')
	{
		if (empty($value))
		{
			return FALSE;
		}
		$sql_select = '*';
		$where = "(user_application_applicant_id='".$id."') AND (user_application_award_id='".$value."') AND (user_application_status = 'Active')";
		$from = 'user_application';
		$query = $this->db->select($sql_select)->where($where)->get($this->db->dbprefix($from));
	    $resultData=array();
		if($query->num_rows()>0){
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	/*public function getFormLabels($value='')
	{
		if (empty($value))
		{
			return FALSE;
		}
		$resultData=array();
		foreach($value as $temp)
		{
			$sql_select = "(master_application_form_name),(master_application_form_label)";
			$sql_whrere = "(master_application_form_name='".$temp."')";
			$from = 'master_application_form';
			$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix($from));
			if($query->num_rows()>0){
				$resultData = $query->result_array();
			}	
		}
		var_dump($resultData);die();
		return $resultData;
	}*/
	
	public function checkUserApplication($value=''){
		if (empty($value))
		{
			return FALSE;
		}
		$sql_select = '*';
		$where = "(user_application_applicant_id='".$value['app_applicant_id']."') AND (user_application_award_id='".$value['app_award_id']."') AND (user_application_status = 'Active')";
		$from = 'user_application';
		$query = $this->db->select($sql_select)->where($where)->get($this->db->dbprefix($from));
		if($query->num_rows()>0){
			return $query->result();
		}
		else {
			return false;
		}
	}
	
	public function checkUserNomination($value=''){
		if (empty($value))
		{
			return FALSE;
		}
		$sql_select = '*';
		$where = "(user_application_applicant_id='".$value['app_applicant_id']."') AND (user_application_award_id='".$value['app_award_id']."') AND (user_application_status = 'Active')";
		$from = 'user_application';
		$query = $this->db->select($sql_select)->where($where)->get($this->db->dbprefix($from));
		if($query->num_rows()>0){
			return $query->result();
		}
		else {
			return false;
		}
	}
	
	public function insertUserFormDetails($value=''){
		if (empty($value))
		{
			return FALSE;
		}
		
		if($value['app_type'] == 'personal')
		{
			$keys = array_keys($value);
			foreach($keys as $temp)
			{
				$new_key = str_replace('ap_', 'user_application_', $temp);
				$val = $value[$temp];
				$data_personal[$new_key] = $val;
			}
		
			unset($data_personal['app_award_id']);
			unset($data_personal['app_type']);
			unset($data_personal['app_applicant_id']);
			unset($data_personal['app_submit']);
			$data_personal['user_application_applicant_id'] = $value['app_applicant_id'];
			$data_personal['user_application_award_id'] =  $value['app_award_id'];
			$data_personal['user_application_application_status'] = 'Incomplete';
			$data_personal['user_application_created_date'] =  date('Y-m-d H:i:s');
			$data_personal['user_application_modified_date'] =  date('Y-m-d H:i:s');
			$data_personal['user_application_status'] =  'Active';
			$result = $this->common_model->insertData('user_application',$data_personal);
		}
		else if($value['app_type'] == 'education')
		{
			$keys = array_keys($value);
			foreach($keys as $temp)
			{
				$new_key = str_replace('ap_', 'user_application_', $temp);
				$val = $value[$temp];
				$data_personal[$new_key] = $val;
			}
		
			unset($data_personal['app_award_id']);
			unset($data_personal['app_type']);
			unset($data_personal['app_applicant_id']);
			$data_personal['user_application_application_status'] = 'Incomplete';
			$data_personal['user_application_modified_date'] =  date('Y-m-d H:i:s');
			$data_personal['user_application_status'] =  'Active';
			$this->db->where(array('user_application_applicant_id'=>$value['app_applicant_id'],'user_application_award_id'=>$value['app_award_id']));
			$result = $this->db->update($this->db->dbprefix('user_application'),$data_personal);
		}
		else if($value['app_type'] == 'document')
		{
			$keys = array_keys($value['files']);
			foreach($keys as $temp)
			{
				$new_key = str_replace('ap_', 'user_application_', $temp);
				$val = $value['files'][$temp];
				
				foreach($value['files'] as $files)
				{
					$config['upload_path'] = UPLOAD_URL.'/application_form_documents/';
					$config['allowed_types'] = 'doc|docx|xlsx|xls|pdf';
					$config['overwrite'] = TRUE;
					$config['max_size']	='2048';
					$config['file_name'] = preg_replace("/[^a-zA-Z0-9.]/", "_", $files['name']);
					$file_name = $config['upload_path'].$config['file_name']; 
					$this->load->library('upload', $config);
					if (isset($files['name']) && !empty($files['name'])){
						move_uploaded_file($files["tmp_name"],$file_name);
					}
					$data_personal[$new_key] = $file_name;
				}
				if(isset($value['app_applicant_refree']) && $value['app_applicant_refree'] > 0)
				{
					$data_personal['user_application_application_status'] = 'Incomplete';
				}
				else
				{
					$data_personal['user_application_application_status'] = 'Complete';	
				}
				$data_personal['user_application_modified_date'] =  date('Y-m-d H:i:s');
				$data_personal['user_application_status'] =  'Active';
				$this->db->where(array('user_application_applicant_id'=>$value['app_applicant_id'],'user_application_award_id'=>$value['app_award_id']));
				$result = $this->db->update($this->db->dbprefix('user_application'),$data_personal);			
			}
		}
		else if($value['app_type'] == 'refree')
		{
			$data_personal1['user_application_application_status'] = 'Complete';	
			$this->db->where(array('user_application_applicant_id'=>$value['app_applicant_id'],'user_application_award_id'=>$value['app_award_id']));
			$this->db->update($this->db->dbprefix('user_application'),$data_personal1);
			
			for($i=0;$i<=count($value['refree_name'])-1;$i++){
				$data_personal['user_application_refree_name']  = $value['refree_name'][$i];
				$data_personal['user_application_refree_email']  = $value['refree_email'][$i];
				$data_personal['user_application_refree_applicant_id'] = $value['app_applicant_id'];
				$data_personal['user_application_refree_application_id'] = $this->session->userdata('user_application_id');
				$data_personal['user_application_refree_verify_status'] = 'Not Verified';
				$data_personal['user_application_refree_created_date'] = date('Y-m-d H:i:s');
				$data_personal['user_application_refree_status'] =  'Active';
				$result[$i] = $this->common_model->insertData('user_application_refree',$data_personal);
			}
			$this->session->unset_userdata('user_application_id');
		}
		//echo $this->db->last_query();die();
		if(isset($result) && $result > 0)
		{
			return $result;
		}
		else {
			return false;
		}
	}

	public function getUserAward($value='', $type='')
	{
		if (empty($value))
		{
			return FALSE;
		}
		if(isset($type) && $type=='All')
		{
			$sql_whrere = "(ua.user_application_award_id=a.award_id) AND (a.award_event_id=e.event_id) AND (ua.user_application_applicant_id = '".$value."') AND (ua.user_application_status = 'Active') order by ua.user_application_created_date desc";
			$sql_select = array('*');
			$from = 'user_application ua, event e, award a';
		}
		else{
			$sql_whrere = "(user_application_applicant_id = '".$value."') AND (user_application_application_status = 'Complete') AND (user_application_status = 'Active')";
			$sql_select = array('*');
			$from = 'user_application';
		}
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix($from));
	    $resultData=array();
		if($query->num_rows()>0){
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	public function getAwardDetail($award_id){
		$this->db->where('award_id',$award_id);
		$query = $this->db->get('scopus_award');
		return $query->result_array();
	}
	
	public function getApplicantDetail($applicant_id){
		$this->db->where('user_application_applicant_id',$applicant_id);
		$query = $this->db->get('scopus_user_application');
		return $query->result_array();
	}
	
	public function validateEventAward($event='', $award='')
	{
		if (empty($event))
		{
			return FALSE;
		}
		$sql_select = '*';
		$where = "(award_id='".$award."') AND (award_event_id='".$event."') AND (award_status = 'Active') AND (award_current_status != 'Done')";
		$from = 'award';
		$query = $this->db->select($sql_select)->where($where)->get($this->db->dbprefix($from));
		if($query->num_rows()>0){
			return $query->result();
		}
		else {
			return false;
		}
	}
	
	public function checkKey_refree($key){
		$this->db->where('user_application_refree_id',$key);
		$this->db->where('user_application_refree_verify_status','Not Verified');
		$query = $this->db->get('scopus_user_application_refree');
		if($query->num_rows()>0){
			return $query->result();
		}else {
			return false;
		}
	}
	
	public function getInfo_Applicant($applicant_id){
		$this->db->where('user_application_applicant_id',$applicant_id);
		$this->db->where('user_application_application_status','Complete');
		$this->db->where('user_application_status','Active');
		$query = $this->db->get('scopus_user_application');
		return $query->result();
	}
	
	public function getInfo_App($applicant_id){
		$this->db->where('user_id',$applicant_id);
		$this->db->where('user_status','Active');
		$query = $this->db->get('scopus_user');
		return $query->result();
	}
	
	public function insertUserNominationFormDetails($value=''){
		if (empty($value))
		{
			return FALSE;
		}
		
		if($value['app_type'] == 'nominating')
		{
			$keys = array_keys($value);
			foreach($keys as $temp)
			{
				$new_key = str_replace('nom_', 'user_nomination_', $temp);
				$val = $value[$temp];
				$data_personal[$new_key] = $val;
			}
		
			unset($data_personal['app_award_id']);
			unset($data_personal['app_event_id']);
			unset($data_personal['app_type']);
			unset($data_personal['app_submit']);
			$data_personal['user_nomination_nominating_id'] = '0';
			$data_personal['user_nomination_award_id'] =  $value['app_award_id'];
			$data_personal['user_nomination_nomination_status'] = 'Incomplete';
			$data_personal['user_nomination_created_date'] =  date('Y-m-d H:i:s');
			$data_personal['user_nomination_modified_date'] =  date('Y-m-d H:i:s');
			$data_personal['user_nomination_status'] =  'Active';
			$result = $this->common_model->insertData('user_nomination',$data_personal);
			$this->session->set_userdata('nom_insert_id', $this->db->insert_id());
		}
		else if($value['app_type'] == 'nominee')
		{
			$keys = array_keys($value);
			foreach($keys as $temp)
			{
				$new_key = str_replace('nom_', 'user_nomination_', $temp);
				$val = $value[$temp];
				$data_personal[$new_key] = $val;
			}
		
			unset($data_personal['app_award_id']);
			unset($data_personal['app_event_id']);
			unset($data_personal['app_type']);
			$data_personal['user_nomination_nomination_status'] = 'Incomplete';
			$data_personal['user_nomination_modified_date'] =  date('Y-m-d H:i:s');
			$data_personal['user_nomination_status'] =  'Active';
			$this->db->where(array('user_nomination_award_id'=>$value['app_award_id'], 'user_nomination_id' => $this->session->userdata('nom_insert_id')));
			$result = $this->db->update($this->db->dbprefix('user_nomination'),$data_personal);
		}
		else if($value['app_type'] == 'disclosure')
		{
			$keys = array_keys($value);
			foreach($keys as $temp)
			{
				$new_key = str_replace('nom_', 'user_nomination_', $temp);
				$val = $value[$temp];
				if(is_array($val))
				{
					$val = implode(', ', $val);
				}
				$data_personal[$new_key] = $val;
			}
		
			unset($data_personal['app_award_id']);
			unset($data_personal['app_event_id']);
			unset($data_personal['app_type']);
			$data_personal['user_nomination_nomination_status'] = 'Complete';
			$data_personal['user_nomination_modified_date'] =  date('Y-m-d H:i:s');
			$data_personal['user_nomination_status'] =  'Active';
			$this->db->where(array('user_nomination_award_id'=>$value['app_award_id'], 'user_nomination_id' => $this->session->userdata('nom_insert_id')));
			$result = $this->db->update($this->db->dbprefix('user_nomination'),$data_personal);
		}
		//echo $this->db->last_query();die();
		if(isset($result) && $result > 0)
		{
			return $result;
		}
		else {
			return false;
		}
	}
	
	public function get_userAppdata(){
		$this->db->where('user_application_award_id',$this->session->userdata('application_award_id'));
		$this->db->where('user_application_applicant_id',$this->session->userdata('user_id'));
		$query = $this->db->get('scopus_user_application');
		//$result[] = $query->result();
		//var_dump($result); die();
		return $query->result();
	}
	
	public function get_userRefreedata(){
		$this->db->where('user_application_refree_applicant_id',$this->session->userdata('user_id'));
		$query = $this->db->get('scopus_user_application_refree');
		//$result[] = $query->result();
		//var_dump($result); die();
		return $query->result();
	}
}
/* End of file common_model.php */
/* Location: ./application/models/common_model.php */